#pragma once

#include "PreInclude.h"

namespace Cactus
{
	class COMMONUTIL_API CommandLineParser
	{
	public:

		/*------------------------------------------------------
		parse the command line into switches and arguments.
		returns number of switches found
		------------------------------------------------------*/
		int				Parse(int argc, char **argv);

		/*------------------------------------------------------
		was the switch found on the command line ?

		example :
		command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
		call                          return
		cmdLine.HasSwitch("-a")       true
		cmdLine.HasSwitch("-z")       false
		------------------------------------------------------*/   
		bool			HasSwitch(const char *pSwitch);

		/*------------------------------------------------------
		fetch an argument associated with a switch . if the parameter at
		index iIdx is not found, this will return the default that you
		provide.

		example :
		command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
		call                                      return
		cmdLine.GetSafeArgument("-a", 0, "zz")    p1
		cmdLine.GetSafeArgument("-a", 1, "zz")    p2
		cmdLine.GetSafeArgument("-b", 0, "zz")    p4
		cmdLine.GetSafeArgument("-b", 1, "zz")    zz
		------------------------------------------------------*/

		Cactus::String	GetSafeArgument(const char *pSwitch, const char *pDefault = 0, int iIdx = 0);

		/*------------------------------------------------------
		fetch a argument associated with a switch. throws an exception 
		of (int)0, if the parameter at index iIdx is not found.

		example :
		command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
		call                             return
		cmdLine.GetArgument("-a", 0)     p1
		cmdLine.GetArgument("-b", 1)     throws (int)0, returns an empty string
		------------------------------------------------------*/
		Cactus::String	GetArgument(const char *pSwitch, int iIdx = 0); 

		/*------------------------------------------------------
		returns the number of arguments found for a given switch.
		returns -1 if the switch was not found
		------------------------------------------------------*/
		int				GetArgumentCount(const char *pSwitch);

	protected:

		/*------------------------------------------------------
		protected member function
		test a parameter to see if it's a switch :

		switches are of the form : -x
		where 'x' is one or more characters.
		the first character of a switch must be non-numeric!
		------------------------------------------------------*/
		bool        IsSwitch(const char *pParam);

		typedef Cactus::map<Cactus::String, Cactus::StringVector >::type	SwitchValueMapType;
		SwitchValueMapType	_Switches;
	};
}